//
//  ProductUIV.m
//  mbstore
//
//  Created by huangyi on 14-3-17.
//  Copyright (c) 2014年 huangyi. All rights reserved.
//

#import "ProductUIV.h"

#import "TreeUIM.h"
#import "ProductDetail.h"
@implementation ProductUIV

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        // Initialization code
    }
    return self;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView==self.upperScrollView) {
        int offset=scrollView.contentOffset.x;
        int page=offset/CGRectGetWidth(self.upperScrollView.bounds);
        NSDictionary *itemId=[_upperItems objectAtIndex:page];
        self.upperBuyButton.payloadObject=itemId;
         [self GetUPDtail:itemId[@"colorSn"]];
    }
    if (scrollView==self.lowerScrollView) {
        int offset=scrollView.contentOffset.x;
        int page=offset/CGRectGetWidth(self.lowerScrollView.bounds);
        NSDictionary *itemId=[_lowerItems objectAtIndex:page];
        [self GetDownDtail:itemId[@"colorSn"]];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    scrollView.showsHorizontalScrollIndicator=NO;
    scrollView.showsVerticalScrollIndicator=NO;
    if (scrollView==self.upperScrollView) {
        CGRect vis=CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, CGRectGetWidth(scrollView.bounds), CGRectGetHeight(scrollView.bounds));
        for (id view in scrollView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=view;
                if (CGRectIntersectsRect(vis, imageView.frame)) {
                    int idx=imageView.tag;
                    NSDictionary *itemId=[_upperItems objectAtIndex:idx];
//                      #warning 调用数据库
//                    NSDictionary *item=[AppUI getProductDict:itemId];
                    NSString *imageName=itemId[@"mainPhoto"];
                    imageName = [imageName stringByDeletingPathExtension];
                    imageName = [NSString stringWithFormat:@"%@--w_450_h_450.jpg",imageName];
                    NSString *imageURL=[AppUI getImageURL:imageName];
                    [imageView setImageURL:imageURL];
                   
                }else{
                    imageView.image=nil;
                }
            }
        }
    }
    
    if (scrollView==self.lowerScrollView) {
        CGRect vis=CGRectMake(scrollView.contentOffset.x, scrollView.contentOffset.y, CGRectGetWidth(scrollView.bounds), CGRectGetHeight(scrollView.bounds));
        for (id view in scrollView.subviews) {
            if ([view isKindOfClass:[UIImageView class]]) {
                UIImageView *imageView=view;
                if (CGRectIntersectsRect(vis, imageView.frame)) {
                    int idx=imageView.tag;
                     NSDictionary *itemId=[_lowerItems objectAtIndex:idx];
//                      #warning 调用数据库
//                    NSDictionary *item=[AppUI getProductDict:itemId];
                    NSString *imageName=itemId[@"mainPhoto"];
                    imageName = [imageName stringByDeletingPathExtension];
                    imageName = [NSString stringWithFormat:@"%@--w_450_h_450.jpg",imageName];
                    NSString *imageURL=[AppUI getImageURL:imageName];
                    [imageView setImageURL:imageURL];
                    
                }else{
                    imageView.image=nil;
                }
            }
        }
    }
    scrollView.showsHorizontalScrollIndicator=YES;
    scrollView.showsVerticalScrollIndicator=YES;
}

-(IBAction)onTypeClick:(id)sender{
    UIButton *button=sender;
    TreeUIM *tree=[[TreeUIM alloc] init];
    tree.delegate=self;
    [self.delegate presentWithNavigationController:tree];
    button.payloadObject=tree;
}

-(IBAction)onBuyClick:(id)sender{
    UIButton *buy=sender;
    NSString *itemId=buy.payloadObject;
    if (itemId) {
        ItemUI *item=self.delegate;
        item.productId=itemId;
    }
}
-(void)setStrSuit:(NSString *)strSuit{
    _strSuit=strSuit;
}
-(void)setDelegate:(ItemUI *)delegate{
     _delegate=delegate;
    [self.vieContainerTotal.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.vieContainerTotal.layer setBorderWidth:1];
    [self.vieContainer.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.vieContainer.layer setBorderWidth:1];
    [self.vieContainerDown.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.vieContainerDown.layer setBorderWidth:1];
    self.labStartPrice.strikeThroughEnabled=YES;
    
    
    [self.btnDown setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.btnDown setTintColor:[UIColor darkGrayColor]];
    [self.btnDown.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.btnDown.layer setBorderWidth:1];
    
    [self.btnUp setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self.btnUp setTintColor:[UIColor darkGrayColor]];
    [self.btnUp.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [self.btnUp.layer setBorderWidth:1];
    
//    上装
   
    upperDetail=[[ProductDetail alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.btnUp.frame)-260, CGRectGetMaxY(self.btnUp.frame)+5, 260, 380)];
    upperDetail.itemdelegate=_delegate;
    
    upperDetail.strSuit=@"上装";
    upperDetail.alpha=0;
    upperDetail.hidden=YES;
    [self addSubview:upperDetail];
    
   
//   下装
    lowerDetail=[[ProductDetail alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(self.btnDown.frame)+5, 260, 380)];
    lowerDetail.itemdelegate=_delegate;
   

    lowerDetail.hidden=YES;
    lowerDetail.alpha=0;
    lowerDetail.strSuit=@"下装";
    [self addSubview:lowerDetail];
   
    responseCtr=[[MBStoreSrv alloc]init];
   
    [self setProductView];

}

-(void)setUpperItems:(NSArray *)upperItems{
    _upperItems=upperItems;

    [self.upperScrollView removeAllSubviews];
    self.upperScrollView.contentOffset=CGPointZero;
    for (int i=0; i<upperItems.count; i++) {
       
//          #warning 调用数据库
//        NSDictionary *item=[AppUI getProductDict:itemId];
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.tag=i;
        imageView.contentMode=UIViewContentModeScaleAspectFit;
        imageView.frame=CGRectOffset(self.upperScrollView.bounds, CGRectGetWidth(self.upperScrollView.bounds)*i, 0);
        [self.upperScrollView addSubview:imageView];
        if (i==0) {
             NSDictionary *itemId=[_upperItems objectAtIndex:i];
            [self GetUPDtail:itemId[@"colorSn"]];

        }
    }
    
    self.upperScrollView.contentSize=CGSizeMake(CGRectGetWidth(self.upperScrollView.bounds)*upperItems.count, CGRectGetHeight(self.upperScrollView.bounds));
    [self.upperScrollView flashScrollIndicators];
    [self scrollViewDidScroll:self.upperScrollView];
}
-(void)setLowerItems:(NSArray *)lowerItems{
    _lowerItems=lowerItems;

    [self.lowerScrollView removeAllSubviews];
    self.upperScrollView.contentOffset=CGPointZero;
    for (int i=0; i<lowerItems.count; i++) {
        UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectZero];
        imageView.tag=i;
        imageView.contentMode=UIViewContentModeScaleAspectFit;
        imageView.frame=CGRectOffset(self.lowerScrollView.bounds, CGRectGetWidth(self.lowerScrollView.bounds)*i, 0);
        
        [self.lowerScrollView addSubview:imageView];
        if (i==0) {
            NSDictionary *itemId=[_lowerItems objectAtIndex:i];
            [self GetDownDtail:itemId[@"colorSn"]];
        }
    }
    
    self.lowerScrollView.contentSize=CGSizeMake(CGRectGetWidth(self.lowerScrollView.bounds)*lowerItems.count, CGRectGetHeight(self.lowerScrollView.bounds));
    [self.lowerScrollView flashScrollIndicators];
    [self scrollViewDidScroll:self.lowerScrollView];
}
//上装下装相信信息
-(void)setProductView{
    ItemUI *item=_delegate;
     item.catId=152;
    [item GoodsDict:^(NSDictionary *dicData) {
        NSLog(@"%@",dicData);
        NSArray *upper=dicData[@"rData"];
            self.upperItems=upper;
    }];
    item.catId=127;
    [item GoodsDict:^(NSDictionary *dicData) {
        NSArray *lower=dicData[@"rData"];
        self.lowerItems=lower;
        NSLog(@"%@",dicData);
    }];

}
-(void)aTreeUIM:(TreeUIM*)uim onItemClick:(NSString *)items{
    ItemUI *item=_delegate;
    if ([_strSuit isEqual:@"上装"]) {
        item.catId=[items integerValue];
        [item GoodsDict:^(NSDictionary *dicData) {
            NSArray *upper=dicData[@"rData"];
            self.upperItems=upper;

        }];
    }
    if ([_strSuit isEqual:@"下装"]) {
        item.catId=[items integerValue];
        [item GoodsDict:^(NSDictionary *dicData) {
            NSArray *lower=dicData[@"rData"];
           self.lowerItems=lower;
            NSLog(@"%@",dicData);
        }];
    }
}
//-(void)setUpperItemsWithType:(NSArray*)type{
//
//    self.upperItems=type;
//}
//-(void)setLowerItemsWithType:(NSArray*)type{
//    self.lowerItems=type;
//}

- (IBAction)PressUp:(UIButton *)sender {
   
    if (upperDetail.hidden) {
        upperDetail.hidden=NO;
        [self ShowView:upperDetail];
//        upperDetail.dicDetail=upDicPro;
        [sender setTitle:@"-上装信息" forState:UIControlStateNormal];
    }
    else{
        [self HideView:upperDetail];
        [sender setTitle:@"+上装信息" forState:UIControlStateNormal];
    }
   
   
}
- (IBAction)PressDown:(UIButton *)sender {
    
    if (lowerDetail.hidden) {
        lowerDetail.hidden=NO;
        [self ShowView:lowerDetail];
//         lowerDetail.dicDetail=lowDicPro;
        [sender setTitle:@"-下装信息" forState:UIControlStateNormal];
    }
    else{
        [self HideView:lowerDetail];
        [sender setTitle:@"+下装信息" forState:UIControlStateNormal];
    }
   
    
}
-(void)ShowView :(UIView *)view{
    [UIView animateWithDuration:0.3 animations:^{
        view.alpha=1.0;
        
    } completion:^(BOOL finished) {
        
    }];
}
-(void)HideView:(UIView *)view{
    [UIView animateWithDuration:0.3 animations:^{
        view.alpha=0.0;
    } completion:^(BOOL finished) {
        view.hidden=YES;
    }];
    
}
//获取商品详情
-(void)GetUPDtail:(NSString *)proId{
    
    [responseCtr getGoodsInfo:1 ColorSn:proId Success:^(NSDictionary *arrReceiveData) {
        upperDetail.dicDetail=arrReceiveData;
//        upDicPro=arrReceiveData;
        NSLog(@"%@",arrReceiveData);
    } Failure:^(NSError *error) {
        
    }];
}
-(void)GetDownDtail:(NSString *)proId{
    
    [responseCtr getGoodsInfo:1 ColorSn:proId Success:^(NSDictionary *arrReceiveData) {
//        lowDicPro=arrReceiveData;
        lowerDetail.dicDetail=arrReceiveData;
    } Failure:^(NSError *error) {
        
    }];
}
@end
